//
//  ZBRouter.m
//  BaseRACDemo
//
//  Created by 庄波 on 2018/3/7.
//  Copyright © 2018年 庄波. All rights reserved.
//

#import "ZBRouter.h"

@interface ZBRouter ()

/// viewModel到viewController的映射
@property (nonatomic, copy) NSDictionary *viewModelViewMappings;

@end


@implementation ZBRouter

static ZBRouter *sharedInstance_ = nil;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [super allocWithZone:zone];
    });
    return sharedInstance_;
}

- (id)copyWithZone:(NSZone *)zone
{
    return sharedInstance_;
}

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [[self alloc] init];
    });
    return sharedInstance_;
}

- (ZBViewController *)viewControllerForViewModel:(ZBViewModel *)viewModel {
    NSString *viewController = self.viewModelViewMappings[NSStringFromClass(viewModel.class)];
    
    NSParameterAssert([NSClassFromString(viewController) isSubclassOfClass:[ZBViewController class]]);
    NSParameterAssert([NSClassFromString(viewController) instancesRespondToSelector:@selector(initWithViewModel:)]);
    
    return [[NSClassFromString(viewController) alloc] initWithViewModel:viewModel];
}


/// 这里是viewModel -> ViewController的映射
/// If You Use Push 、 Present 、 ResetRootViewController ,You Must Config This Dict
- (NSDictionary *)viewModelViewMappings {
    return @{@"ZBOneModel":@"ZBOneViewC",
             @"ZBHomePageViewModel":@"ZBHomeViewC",
             @"ZBTwoModel":@"ZBTwoViewC",
             @"ZBThreeModel":@"ZBThreeViewC",
             @"ZBFourModel":@"ZBFourViewC",
             @"ZBOneSubModel":@"ZBOneSubViewC",
             @"ZBLoginViewModel":@"ZBLoginViewController",
             
             @"ZBLinkmanViewModel":@"ZBLinkmanViewC",
             @"ZBOneSendMessageModel":@"ZBOneSendMessage",
             @"ZBOwnViewModel":@"ZBOwnViewC",
             @"ZBResultViewModel":@"ZBResultViewC",
             };
}

@end
